<?php session_start();
require('globals/setup.php');
$smarty = new Smarty_Kalender;	

$host  = $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_NAME'] == 'localhost'){$uri= URI_L;}else{$uri= URI_P;}
if (!isset($_SESSION['id_user']) || $_SESSION['id_user']=='') {
	echo "<script language=Javascript> location.href='http://$host$uri/index.php'; </script>";	
}else{
	
	if (!is_object($bd)){	
		require("globals/Db.class.php");
		require("globals/Conf.class.php");
		$bd=Db::getInstance();	
	}
	if (!is_object($util)){	
		include("globals/Util.class.php");		
		$util=Util::getInstance();
	}
	
	//$util->debugOn();

	if (!($util->administrarTiempoSesion())){
		echo "<script language=Javascript> location.href='http://$host$uri/timeout.php'; </script>";
	}else if (!($util->administrarAcceso("addCustomer"))){
		echo "<script language=Javascript> location.href='http://$host$uri/sinAcceso.php'; </script>";
	}
	
	function is_valid_email($email){
	  $result = true;
	  if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
	    $result = false;
	  }
	  return $result;
	}
	
	
	$smarty->assign("titleHTML", "Kalender - Agregar Cliente / Usuario");
	$smarty->assign("titulo", "Agregar Cliente / Usuario");
	$smarty->assign("update", 'false');
	if (isset($_GET["client"])){
		$smarty->assign("update", 'true');
		$smarty->assign("disabled", "disabled='disabled'");
		$smarty->assign("titleHTML", "Kalender - Modificar Cliente / Usuario");
		$smarty->assign("titulo", "Modificar Cliente / Usuario");
		
		$query_clientes = "select id, super_cliente, tipo, usuario, password, localidad, codigo_postal, fax, pais, nivel, "
		."razon_social, cuit, email, observaciones, contacto, direccion, telefono, logo, permisos from usuarios where id = ".$_GET["client"].";";
		$resultado = $bd->eje($query_clientes);		
		$num_rows = $bd->numRows($resultado);
		if ($num_rows == 1){
			$row = $bd->fila($resultado);
			$smarty->assign("id_update", $row["id"]);
			$smarty->assign("razon_social", $row["razon_social"]);
			$smarty->assign("direccion", $row["direccion"]);
			$smarty->assign("cuit", $row["cuit"]);
			$smarty->assign("tipo", $row["tipo"]);
			$smarty->assign("pais", $row["pais"]);
			$smarty->assign("contacto", $row["contacto"]);
			$smarty->assign("localidad", $row["localidad"]);
			$smarty->assign("codigo_postal", $row["codigo_postal"]);
			$smarty->assign("fax", $row["fax"]);
			$smarty->assign("email", $row["email"]);
			$smarty->assign("telefono", $row["telefono"]);
			$smarty->assign("logo", $row["logo"]);
			$smarty->assign("observaciones", $row["observaciones"]);
			$smarty->assign("usuario", $row["usuario"]);
			$smarty->assign("password", $row["password"]);
			$smarty->assign("super", $row["super_cliente"]);
			$smarty->assign("super_SI", "");
			$smarty->assign("super_NO", " checked='checked' ");
			if ($row["super_cliente"]=='1'){
				$smarty->assign("super_SI", " checked='checked' ");
				$smarty->assign("super_NO", "");
			}
			
			//aca por el tema de los permisos hago una aclaracion: Como los usuarios ahora son solo nivel 1, si este usuario tiene permisos admin (*), 
			//entonces mando $nivelUser=0, sino $nivelUser=1 [Ver crearUsuario.tpl para mayor info.]
			if ($row["permisos"]=='*'){
				$smarty->assign("nivelUser", "0");
			}else{
				$smarty->assign("nivelUser", $row["nivel"]);
			}
		}
	}else{
		$smarty->assign("super_SI", "");
		$smarty->assign("super_NO", " checked='checked' ");
		$smarty->assign("nivelUser", 3);
	}
	
	$paises_options = array();	
	$result_countries = $bd->eje("SELECT id,name from countries");
	while ($row1 = $bd->fila($result_countries)){										
		$id = $row1["id"];
		$name = $row1["name"];		
		if ($pais == $name){
			array_push($paises_options, "<option value='".$name."' selected>".$name."</option>");
		}else{
			array_push($paises_options, "<option value='".$name."'>".$name."</option>");
		}
	}
	$smarty->assign("paises_options", $paises_options);
	$smarty->assign("paises_options_cant", sizeof($paises_options));
	
	$smarty->assign("menuItem", "addCustomer");
	include("globals/menuIzquierda.php");	
	
	$smarty->display("agregarCliente.tpl");
	
} ?>